<template>
  <div class="subject">
    <span>{{ a }}</span>
    <span>+</span>
    <span>{{ b }}</span>
    <span>=</span>
    <input type="number" v-model.number="answer" :disabled="dis" />
    <button @click.prevent="ck" :disabled="dis">提交</button>
  </div>
</template>

<script>
export default {
  props: ["index"],
  data() {
    return {
      a: Math.floor(Math.random() * 100),
      b: Math.floor(Math.random() * 100),
      answer: "",
      dis: false,
    };
  },
  methods: {
    ck() {
      let flag;
      if (this.answer === this.result) flag = "正确";
      else flag = "错误";
      this.$bus.$emit(`sub${this.index}`, flag);
      this.dis = true;
    },
  },
  computed: {
    result() {
      return this.a + this.b;
    },
  },
};
</script>

<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}
.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.subject input {
  width: 50px;
  height: 20px;
}
</style>